<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="广告类型" prop="groupId">
        <el-select v-model="queryParams.groupId" placeholder="广告类型"  size="small">
          <el-option label="全部"
                     key="0"
                     value="0">
          </el-option>
          <el-option
            v-for="dict in bannerType"
            :key=dict.dictValue
            :label="dict.dictLabel"
            :value=dict.dictValue
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openAddDialog"
          v-hasPermi="['shop:banner:add']"
        >添加
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bannerList">
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="描述" align="center" prop="description"/>
      <el-table-column label="状态" align="center" key="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value='1'
            :inactive-value='0'
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="广告类型" :formatter="groupFormat" align="center" prop="groupId"/>
      <el-table-column label="跳转到" :formatter="redirectTypeFormat" align="center" prop="redirectType"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="openUpdateDialog(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :hide-on-single-page="true"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @size-change="sizeChange"
      @current-change="pageChange"
    />

    <el-dialog
      :visible.sync="open"
      :title="title"
      width="40%" append-to-body
    >
      <el-form  :rules="rules" :model="formData" label-width="120px" ref="form">
        <el-row>
          <el-col :span="20">
            <el-form-item label="广告名称" prop="name">
              <el-input v-model="formData.name" placeholder="请输入广告名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="广告描述" prop="description">
              <el-input v-model="formData.description" placeholder="请输入广告描述"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="选择广告位" prop="groupId">
              <el-select v-model="formData.groupId" placeholder="广告类型"  size="small">
                <el-option
                  v-for="dict in bannerType"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="跳转到" prop="redirectType">
              <el-select v-model="formData.redirectType" placeholder="跳转到"  size="small">
                <el-option
                  v-for="dict in redirectType"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item v-show="formData.redirectType==1" label="选择商品" prop="keyStr">
              <el-select v-show="formData.redirectType==1" v-model="formData.keyStr" placeholder="选择商品"  size="small">
                <el-option
                  v-for="dict in bannerType"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="广告图片" prop="imageUrl">
              <picture-manager width="150" height="150" :value="formData.imageUrl">
              </picture-manager>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="handleAddOrUpdate">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import PictureManager from "@/components/ImageUpload/PictureManager";
import * as bannerApi from "@/api/shop/banner.js";

export default {
  name: "ShopBanner",
  components: {
    PictureManager
  },
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: null
      },
      showSearch: true,
      total: 0,
      //广告列表数据
      bannerList: [],
      //广告类型字典
      bannerType: [],
      redirectType: [],
      // 遮罩层
      loading: false,
      //是否打开遮罩层
      open: false,
      //弹出框的标题
      title: '',
      //提交或修改时的表单内容
      formData: {
        imageUrl: []
      },
      //表单校验规则
      rules: {
        name: [{required: true, message: "广告名称不能为空", trigger: "blur"}],
        description: [{required: true, message: "广告描述不能为空", trigger: "blur"}],
        imageUrl: [{required: true, message: "图片必选", trigger: "blur"}]
      }
    }
  }
  ,
  computed: {},
  created() {
    this.getList();
    this.getDicts("banner_type").then(response => {
      this.bannerType = response.data;
    });
    this.getDicts("redirect_type").then(response => {
      this.redirectType = response.data;
    });
  }
  ,
  methods: {
    getList() {
      bannerApi.bannerList(this.queryParams).then((res) => {
        this.total = res.total;
        this.bannerList = res.rows
      });
    }
    ,
    groupFormat(row) {
      return this.selectDictLabel(this.bannerType, row.groupId);
    }
    ,
    redirectTypeFormat(row) {
      return this.selectDictLabel(this.redirectType, row.redirectType);
    }
    ,
    handleStatusChange(row) {
      let text = row.status === 1 ? "启用" : "停用";
      this.$confirm('确认要' + text + '本条广告吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return bannerApi.updateOrSave(row);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === 0 ? 1 : 0;
      });
    }
    ,
    sizeChange(val) {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = val
      this.getList()
    }
    ,
    pageChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    }
    ,
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
    ,
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    }
    ,

    handleDelete(row) {
      this.$confirm('确认要删除本条广告吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return bannerApi.deleteObj(row.id);
      }).then(() => {
        this.msgSuccess("删除成功");
        this.getList();
      }).catch(function () {
      });
    }
    ,
    handleAddOrUpdate() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let temp = Object.assign({}, this.formData);
          let param = Object.assign(temp, {imageUrl: this.formData.imageUrl[0]});
          bannerApi.updateOrSave({
            ...temp
          }).then((res) => {
            this.initParamData();
            this.open = false;
            this.getList();
          });
        } else {
          return false;
        }
      });

    }
    , openUpdateDialog(row) {
      this.title = '修改';
      this.open = true;
      this.formData = Object.assign({},row);
      Object.assign(this.formData, {
        imageUrl: [this.formData.imageUrl],
        groupId: this.formData.groupId.toString(),
        redirectType:this.formData.redirectType.toString()
      })
    }
    ,
    openAddDialog() {
      this.title = '添加';
      this.open = true;
      this.initParamData()
    }
    ,
    initParamData() {
      this.formData = {
        groupId: '1',
        redirectType: '0',
        imageUrl: new Array()
      }
    }
  }
}
;
</script>
<style lang="scss" scoped>

</style>
